var pickerFile = require('../tools/js/picker_datetime.js');
function addDatetimeZero(num){
    return new Array(2 - num.toString().length + 1).join("0") + num;
}
var app = getApp()
var nowDate = new Date();
var nowYear = nowDate.getFullYear();
var nowMonth = nowDate.getMonth()+1;
var nowDay = nowDate.getDate();
var nowHour = nowDate.getHours();
var nowMinute = nowDate.getMinutes();
var pickerWeekArr = ["日","一","二","三","四","五","六"];
var nowWeek = pickerWeekArr[nowDate.getDay()];
var now = nowYear+'年'+nowMonth+'月' + nowDay+'日 星期' + nowWeek+' ' + addDatetimeZero(nowHour)+':' + addDatetimeZero(nowMinute)
// edit.js
Page({
    data: {
        partyId : -1,
        remindPickerArray: ['请选择>', 'a', 'b', 'c'],
        typePickerArray:['请选择>','a','b','c'],
        PickerPayTypeArray: ['请选择>', 'AA制', '群主请客'],
        typePicker:0,
        remindPicker: 0,
        PickerPayType:0,
        beginTime: now,
        beginTimeUnix: Math.round(new Date().getTime()/1000),
        location: {
            name: "请选择>"
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        if(options.partyId != undefined && options.partyId!= '' &&options.partyId >0){
            that.setData({
                partyId:options.partyId,
                title:options.title,
                description:options.description,
                beginTime:options.beginTime,
                beginTimeUnix:options.beginTimeUnix,
                maxMember:options.maxMember,
                location:{
                    name:options.locationName,
                    address:options.locationAddress,
                    latitude:options.latitude,
                    longitude:options.longitude
                }
            })
        }else {
            that.setData({
                beginTime:now
            })
        }

        app.getUserInfo(function(userInfo){
            //更新数据
            that.setData({
                userInfo:userInfo,
            })
            console.log(that.data.openId)
        })
        this.$wuxToast = app.wux(this).$wuxToast

        this.datetimePicker = new pickerFile.pickerDatetime({
            page:this,
            animation:'slide',
            duration:500
        })

    },


    startTap : function(){
        this.datetimePicker.setPicker('beginTime');
    },
    bindPickerChange: function(e) {
        this.setData({
            remindPicker: e.detail.value
        })
    },

    bindPickerType: function (e) {
      this.setData({
        typePicker: e.detail.value
      })
    },
    bindPickerPayType: function (e) {
      this.setData({
        PickerPayType: e.detail.value
      })
    },
    locationPicker:function () {
        var that = this
        wx.chooseLocation({
            success: function(res){
                console.log(res)
                if(res.name == '' && res.address != ''){
                    res.name = res.address.replace(/\(.*?\)/g,'')
                }
                that.setData({
                    location:res
                })
                console.log(that.data)
            }
        })
    },
    listenTitle:function (e) {
        this.setData({
            title:e.detail.value
        });
    },
    listenMaxMember:function (e) {
        if(!isNaN(e.detail.value)&&e.detail.value<100){
            this.setData({
                maxMember:e.detail.value
            });
        }else {
            this.setData({
                maxMember:''
            });
        }
    },
    listenDescribe:function (e) {
        this.setData({
            describe:e.detail.value
        });
    },
    startParty:function (e) {
        var _this =this;
        var formId = e.detail.formId;
        if(this.data.title == undefined || this.data.title == ''){
            _this.$wuxToast.show({
                type: 'forbidden',
                timer: 1500,
                color: '#fff',
                text: '请输入主题',
            })
            return false;
        }
        if(this.data.beginTime == undefined || this.data.beginTime == ''){
            _this.$wuxToast.show({
                type: 'forbidden',
                timer: 1500,
                color: '#fff',
                text: '请输入开始时间'
            })
            return false;
        }
        if(this.data.location == undefined || this.data.location.name == '地点'){
            _this.$wuxToast.show({
                type: 'forbidden',
                timer: 1500,
                color: '#fff',
                text: '请输入活动地址'
            })
            return false;
        }
        if(this.data.maxMember == undefined || this.data.maxMember == ''){
            _this.$wuxToast.show({
                type: 'forbidden',
                timer: 1500,
                color: '#fff',
                text: '请输入参与人数'
            })
            return false;
        }
        if(this.data.remindPicker == undefined || this.data.remindPicker == -1){
            _this.$wuxToast.show({
                type: 'forbidden',
                timer: 1500,
                color: '#fff',
                text: '请输入提醒时间'
            })
            return false;
        }
        wx.request({
            url:"https://wxapps.lovemojito.com/mApp/startParty.php",
            data: {
                title:this.data.title,
                beginTime:this.data.beginTime,
                beginTimeUnix:this.data.beginTimeUnix,
                location:this.data.location,
                maxMember:this.data.maxMember,
                remindPicker:this.data.remindPicker,
                description:this.data.describe,
                userInfo:this.data.userInfo,
                partyId:this.data.partyId,
                formId:formId,
                jyj:'jyj'
            },
            header: {
                'content-type': 'application/json'
            },
            success:function (res) {
                console.log(res)
                if(res.data.errCode){
                    _this.$wuxToast.show({
                            type: 'success',
                            timer: 1500,
                            color: '#fff',
                            text: res.data.errMsg,
                            success:function () {
                                wx.navigateTo({
                                    url: '/pages/index/index?partyId='+res.data.partyId
                                })
                            }
                })
                }else {
                    _this.$wuxToast.show({
                            type: 'cancel',
                            timer: 1500,
                            color: '#fff',
                            text: res.data.errMsg
                    })
                }
            }
        })

    }
})